package com.oc.mba.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * a simple wrapper of  a spring container
 * 
 * @author wu at gnet
 * 
 */
public class SpringUtil {

	private static BeanFactory factory;
	
	final static Logger log = LoggerFactory.getLogger(SpringUtil.class);

	static {
		log.debug("init is beginning!");
//		String configs[];
		try {
//			configs = ConfigUtil.getConfig("GNET.SPRING.CFGS").split(",",-1);
			String configs[] ={"applicationContext.xml"};
			factory = new ClassPathXmlApplicationContext(configs);

		} catch (NullPointerException  e) {
			log.error(e.getMessage(),e);			
			log.error("Init spring error:pls check cfg GNET.SPRING.CFGS is set correctly");
		}
		
		//register a shutdown hook with the JVM,so the container to shutdown
		//gracefully and call the relevant destroy callbacks on your singleton beans
		((ClassPathXmlApplicationContext) factory).registerShutdownHook();
		
		
	}

	/**
	 * @deprecated  by registerShutdownHook
	 *
	 */
	public static void stop() {
		//((ClassPathXmlApplicationContext) factory).close();
		
	}

	/**
	 * retrieve a java bean from spring container
	 * 
	 * @param name:the bean name specified in spring configuration xml file
	 * @return
	 */
	public static Object getBean(String name) {
		log.debug("Trace into getBean");		
		Object bean = factory.getBean(name);
		return bean;
	}

}
